<script setup lang="ts">

import {onMounted, ref} from "vue";
import {getBlogList} from "@/api/blog";
import {Toast} from "vant";
import router from "@/router";
import TeamCard from "@/components/TeamCard.vue";
import {getTeamList} from "@/api/team";
import {useUserStore} from "@/store/user";

const onClickLeft = () => {
  router.push("/team/create")
};

const searchText = ref('')
const searchBlog = () => {
  console.log(searchText.value)
}

const list = ref([]);
const loadingPublic = ref(false);
const loadingLock = ref(false);
const finishedPublic = ref(false);
const finishedLock = ref(false);

const currentPagePublic = ref(1);
const currentPageLock = ref(1);
const pageSize = ref(6);
const publicTeamList = ref([]);
const lockTeamList = ref([]);

const user = useUserStore()
const loadTeamList = async (status) => {

  // 异步更新数据
  if (status === 0) {
    const {records} = await getTeamList(currentPagePublic.value, pageSize.value, status, user.id);
    currentPagePublic.value += 1;
    loadingPublic.value = false;
    publicTeamList.value.push(...records)
    console.log("records", records.length)
    if (records.length < pageSize.value) {
      finishedPublic.value = true;
    }
  } else if (status === 2) {
    const {records} = await getTeamList(currentPageLock.value, pageSize.value, status, user.id);
    currentPageLock.value += 1;
    loadingLock.value = false;
    lockTeamList.value.push(...records)
    if (records.length < pageSize.value) {
      finishedLock.value = true;
    }
  }
}


const active = ref(0);


</script>

<template>
  <van-nav-bar left-text="新建" :title="$route.name" @click-left="onClickLeft">
    <template #right>
      <van-icon name="search" size="18"/>
    </template>
  </van-nav-bar>
  <van-notice-bar
      left-icon="bulb-o"
      text="欢迎大家来到厚米匹配ovo，你可以在这里找到学习homie，生活homie甚至是灵魂homie，同时支持匹配相同爱好的homie并且可以和你的homie聊天，但是要遵纪守法喔！最后欢迎大家提出反馈意见！qwq"
  />
  <!--  搜索-->
  <van-search v-model="searchText" placeholder="搜索我的博客" @search="searchBlog"/>
  <!--  公开私密切换-->
  <van-tabs v-model:active="active">
    <van-tab title="公开">
      <van-list
          v-if="active === 0"
          v-model:loading="loadingPublic"
          :finished="finishedPublic"
          finished-text="没有更多了"
          @load="loadTeamList(0)"
      >
        <TeamCard v-for="team in publicTeamList" :team="team"/>
      </van-list>
    </van-tab>
    <van-tab title="加密">
      <van-list
          v-if="active === 1"
          v-model:loading="loadingLock"
          :finished="finishedLock"
          finished-text="没有更多了"
          @load="loadTeamList(2)"
      >
        <TeamCard v-for="team in lockTeamList" :team="team"/>
      </van-list>
    </van-tab>
  </van-tabs>

</template>

<style scoped>

</style>
